<template>
  <div class="comment-post">
    <van-field
      class="post-field"
      v-model="message"
      rows="2"
      autosize
      type="textarea"
      maxlength="50"
      placeholder="请输入留言"
      show-word-limit
      @keyup.enter="onSendComment"
    />
    <van-button
      class="post-btn"
      @click="onSendComment"
      :disabled="!message.length"
    >发布</van-button>
  </div>
</template>

<script>
import { addComment } from '@/api/comment'
export default {
  name: 'CommentPost',
  components: {},
  props: {
    targetId:{
      type:[String,Number,Object],
      required:true
    },
    articleId:{
      type:[String,Number,Object]
    }
  },
  data () {
    return {
      message: '' //绑定数据
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    // 发布评论
    async onSendComment(){
      try {
       const {data:res} = await addComment({
          target:this.targetId.toString(),
          content:this.message,
          art_id:this.articleId ? this.articleId.toString():null
        })
        this.$toast.success('评论成功')
        this.$emit('updateCommentList', res.data.new_obj)
        this.message= ''
      } catch (error) {
        if(error.response.status===401){
          this.$toast.fail('登录才可以评论')
          this.$router.push('/login')
        }else if(error.response.status===403){
          this.$toast.fail('关闭评论了')

        }else{
          this.$toast.fail('评论失败，稍后重试')

        }
      }
    }
  }
}
</script>

<style scoped lang="less">
.comment-post {
  display: flex;
  align-items: center;
  padding: 32px 0 32px 32px;
  .post-field {
    background-color: #f5f7f9;
  }
  .post-btn {
    width: 150px;
    border: none;
    padding: 0;
    color: #6ba3d8;
    &::before {
      display: none;
    }
  }
}
</style>